#ifdef __cplusplus
#include <cstdlib>
#else
#include <stdlib.h>
#endif
#include <SDL/SDL.h>
void deplacer ();
void score ();
void gagnant();
void jouer();
void quitter();
void pause();

int main ( int argc, char** argv )
{ char pause;
    SDL_Event event;

    freopen("CON","w",stdout);
    freopen("CON","r",stdin);
    if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
    {
        printf( "Unable to init SDL: %s\n", SDL_GetError() );
        return 1;
    }

    SDL_Surface*screen;
    SDL_Surface*temp;
    SDL_Surface*image;
    SDL_Rect src,dest;

    screen = SDL_SetVideoMode(800, 700, 32,  SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_RESIZABLE);
    SDL_WM_SetCaption("DOMINO", NULL);

    temp = SDL_LoadBMP("tapis.bmp");
    if (temp == NULL)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 1;
    }

    image = SDL_DisplayFormat(temp);

    SDL_Surface* bmp1 = SDL_LoadBMP("piece1.bmp");
    SDL_Surface* bmp2 = SDL_LoadBMP("piece2.bmp");
    SDL_Surface* bmp3 = SDL_LoadBMP("piece3.bmp");
    SDL_Surface* bmp4 = SDL_LoadBMP("piece4.bmp");
    SDL_Surface* bmp5 = SDL_LoadBMP("piece5.bmp");
    SDL_Surface* bmp6 = SDL_LoadBMP("piece6.bmp");
    SDL_Surface* bmp7 = SDL_LoadBMP("piece7.bmp");
    SDL_Surface* bmp8 = SDL_LoadBMP("piece8.bmp");
    SDL_Surface* bmp9 = SDL_LoadBMP("piece9.bmp");
    SDL_Surface* bmp10 = SDL_LoadBMP("piece10.bmp");
    SDL_Surface* bmp11 = SDL_LoadBMP("piece11.bmp");
    SDL_Surface* bmp12 = SDL_LoadBMP("piece12.bmp");
    SDL_Surface* bmp13 = SDL_LoadBMP("piece13.bmp");
    SDL_Surface* bmp14 = SDL_LoadBMP("piece14.bmp");
    SDL_Surface* bmp15 = SDL_LoadBMP("piece15.bmp");
    SDL_Surface* bmp16 = SDL_LoadBMP("piece16.bmp");
    SDL_Surface* bmp17 = SDL_LoadBMP("piece17.bmp");
    SDL_Surface* bmp18 = SDL_LoadBMP("piece18.bmp");
    SDL_Surface* bmp19 = SDL_LoadBMP("piece19.bmp");
    SDL_Surface* bmp20 = SDL_LoadBMP("piece20.bmp");
    SDL_Surface* bmp21 = SDL_LoadBMP("piece21.bmp");
    SDL_Surface* bmp22 = SDL_LoadBMP("piece22.bmp");
    SDL_Surface* bmp23 = SDL_LoadBMP("piece23.bmp");
    SDL_Surface* bmp24 = SDL_LoadBMP("piece24.bmp");
    SDL_Surface* bmp25 = SDL_LoadBMP("piece25.bmp");
    SDL_Surface* bmp26 = SDL_LoadBMP("piece26.bmp");
    SDL_Surface* bmp27 = SDL_LoadBMP("piece27.bmp");
    SDL_Surface* bmp28 = SDL_LoadBMP("piece28.bmp");

    SDL_Rect dstrect1;
    SDL_Rect dstrect2;
    SDL_Rect dstrect3;
    SDL_Rect dstrect4;
    SDL_Rect dstrect5;
    SDL_Rect dstrect6;
    SDL_Rect dstrect7;
    SDL_Rect dstrect8;
    SDL_Rect dstrect9;
    SDL_Rect dstrect10;
    SDL_Rect dstrect11;
    SDL_Rect dstrect12;
    SDL_Rect dstrect13;
    SDL_Rect dstrect14;
    SDL_Rect dstrect15;
    SDL_Rect dstrect16;
    SDL_Rect dstrect17;
    SDL_Rect dstrect18;
    SDL_Rect dstrect19;
    SDL_Rect dstrect20;
    SDL_Rect dstrect21;
    SDL_Rect dstrect22;
    SDL_Rect dstrect23;
    SDL_Rect dstrect24;
    SDL_Rect dstrect25;
    SDL_Rect dstrect26;
    SDL_Rect dstrect27;
    SDL_Rect dstrect28;

    dstrect1.x = ((screen->w - bmp1->w) / 2)-100;
    dstrect1.y = ((screen->h - bmp1->h) / 2) ;
    dstrect2.x = ((screen->w - bmp2->w) / 2)+100;
    dstrect2.y = ((screen->h - bmp2->h) / 2);
    dstrect3.x = ((screen->w - bmp3->w) / 2)-800;
    dstrect3.y = ((screen->h - bmp3->h) / 2)-190;
    dstrect4.x = ((screen->w - bmp4->w) / 2)-800;
    dstrect4.y = ((screen->h - bmp4->h) / 2)-140;
    dstrect5.x = ((screen->w - bmp5->w) / 2)-800;
    dstrect5.y = ((screen->h - bmp5->h) / 2)-90;
    dstrect6.x = ((screen->w - bmp6->w) / 2)-800;
    dstrect6.y = ((screen->h - bmp6->h) / 2)-40;
    dstrect7.x = ((screen->w - bmp7->w) / 2)-800;
    dstrect7.y = ((screen->h - bmp7->h) / 2)+10;
    dstrect8.x = ((screen->w - bmp8->w) / 2)-800;
    dstrect8.y = ((screen->h - bmp8->h) / 2)+60;
    dstrect9.x = ((screen->w - bmp9->w) / 2)-800;
    dstrect9.y = ((screen->h - bmp9->h) / 2)+110;
    dstrect10.x = ((screen->w - bmp10->w) / 2)-800;
    dstrect10.y = ((screen->h - bmp10->h) / 2)+160;
    dstrect11.x = ((screen->w - bmp11->w) / 2)-800;
    dstrect11.y = ((screen->h - bmp11->h) / 2)+210;
    dstrect12.x = ((screen->w - bmp12->w) / 2)+630;
    dstrect12.y = ((screen->h - bmp12->h) / 2)-300;
    dstrect13.x = ((screen->w - bmp13->w) / 2)+630;
    dstrect13.y = ((screen->h - bmp13->h) / 2)-245;
    dstrect14.x = ((screen->w - bmp14->w) / 2)+630;
    dstrect14.y = ((screen->h - bmp14->h) / 2)-190;
    dstrect15.x = ((screen->w - bmp15->w) / 2)+630;
    dstrect15.y = ((screen->h - bmp15->h) / 2)-140;
    dstrect16.x = ((screen->w - bmp16->w) / 2)+630;
    dstrect16.y = ((screen->h - bmp16->h) / 2)-90;
    dstrect17.x = ((screen->w - bmp17->w) / 2)+630;
    dstrect17.y = ((screen->h - bmp17->h) / 2)-40;
    dstrect18.x = ((screen->w - bmp18->w) / 2)+630;
    dstrect18.y = ((screen->h - bmp18->h) / 2)+10;
    dstrect19.x = ((screen->w - bmp19->w) / 2)+630;
    dstrect19.y = ((screen->h - bmp19->h) / 2)+60;
    dstrect20.x = ((screen->w - bmp20->w) / 2)+630;
    dstrect20.y = ((screen->h - bmp20->h) / 2)+110;
    dstrect21.x = ((screen->w - bmp21->w) / 2)+630;
    dstrect21.y = ((screen->h - bmp21->h) / 2)+160;
    dstrect22.x = ((screen->w - bmp22->w) / 2)+630;
    dstrect22.y = ((screen->h - bmp22->h) / 2)+210;
    dstrect23.x = ((screen->w - bmp23->w) / 2)+630;
    dstrect23.y = ((screen->h - bmp23->h) / 2)+260;
    dstrect24.x = ((screen->w - bmp24->w) / 2)+630;
    dstrect24.y = ((screen->h - bmp24->h) / 2)+310;
    dstrect25.x = ((screen->w - bmp25->w) / 2)-800;
    dstrect25.y = ((screen->h - bmp25->h) / 2)+260;
    dstrect26.x = ((screen->w - bmp26->w) / 2)-800;
    dstrect26.y = ((screen->h - bmp26->h) / 2)+310;
    dstrect27.x = ((screen->w - bmp27->w) / 2)-800;
    dstrect27.y = ((screen->h - bmp27->h) / 2)-300;
    dstrect28.x = ((screen->w - bmp28->w) / 2)-800;
    dstrect28.y = ((screen->h - bmp28->h) / 2)-245;


    SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));


    SDL_Flip(screen);
    int done=1;
    while(done)
    {

        while(SDL_PollEvent(&event))
        {
            switch(event.type)
            {
            case SDL_QUIT:
                done=0;
                break;

            case SDL_KEYDOWN:


                if(event.key.keysym.sym == SDLK_ESCAPE)
                    done=0;
                break;

            case SDL_MOUSEBUTTONDOWN:

                int x=event.motion.x;
                int y=event.motion.y;
                if(x>=(screen->w)/2)
                {

                    dstrect1.x = ((screen->w - bmp1->w) / 2)+100;
                    dstrect2.x = ((screen->w - bmp2->w) / 2)-100;
                }
                if(x<=(screen->w)/2)
                {

                    dstrect1.x = ((screen->w - bmp1->w) / 2)-100;
                    dstrect2.x = ((screen->w - bmp2->w) / 2)+100;
                }

                if(y>=(screen->h)/2)
                {

                    dstrect1.y = ((screen->h - bmp1->h) / 2)+100;
                    dstrect2.y = ((screen->h - bmp2->h) / 2)-100;
                }

                if(y<=(screen->h)/2)
                {

                    dstrect1.y = ((screen->h - bmp1->h) / 2)-100;
                    dstrect2.y = ((screen->h - bmp2->h) / 2)+100;
                }



                break;

            }


            SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 255));

            SDL_BlitSurface(bmp1, 0, screen, &dstrect1);
            SDL_BlitSurface(bmp2, 0, screen, &dstrect2);
            SDL_BlitSurface(bmp3, 0, screen, &dstrect3);
            SDL_BlitSurface(bmp4, 0, screen, &dstrect4);
            SDL_BlitSurface(bmp5, 0, screen, &dstrect5);
            SDL_BlitSurface(bmp6, 0, screen, &dstrect6);
            SDL_BlitSurface(bmp7, 0, screen, &dstrect7);
            SDL_BlitSurface(bmp8, 0, screen, &dstrect8);
            SDL_BlitSurface(bmp9, 0, screen, &dstrect9);
            SDL_BlitSurface(bmp10, 0, screen, &dstrect10);
            SDL_BlitSurface(bmp11, 0, screen, &dstrect11);
            SDL_BlitSurface(bmp12, 0, screen, &dstrect12);
            SDL_BlitSurface(bmp13, 0, screen, &dstrect13);
            SDL_BlitSurface(bmp14, 0, screen, &dstrect14);
            SDL_BlitSurface(bmp15, 0, screen, &dstrect15);
            SDL_BlitSurface(bmp16, 0, screen, &dstrect16);
            SDL_BlitSurface(bmp17, 0, screen, &dstrect17);
            SDL_BlitSurface(bmp18, 0, screen, &dstrect18);
            SDL_BlitSurface(bmp19, 0, screen, &dstrect19);
            SDL_BlitSurface(bmp20, 0, screen, &dstrect20);
            SDL_BlitSurface(bmp21, 0, screen, &dstrect21);
            SDL_BlitSurface(bmp22, 0, screen, &dstrect22);
            SDL_BlitSurface(bmp23, 0, screen, &dstrect23);
            SDL_BlitSurface(bmp24, 0, screen, &dstrect24);
            SDL_BlitSurface(bmp25, 0, screen, &dstrect25);
            SDL_BlitSurface(bmp26, 0, screen, &dstrect26);
            SDL_BlitSurface(bmp27, 0, screen, &dstrect27);
            SDL_BlitSurface(bmp28, 0, screen, &dstrect28);
            image = SDL_DisplayFormat(temp);

            SDL_FreeSurface(temp);

            src.x = 0;
            src.y = 0;
            src.w = image->w;
            src.h = image->h;
            dest.x = 100;
            dest.y = 100;
            dest.w = image->w;
            dest.h = image->h;



            SDL_BlitSurface(image, &src, screen, &dest);
            SDL_FreeSurface(image);


            SDL_Flip(screen);
        }
    }


    SDL_Quit();
    pause = getchar();
    return 0;
}
